package cn.interestingshop.web.manager;

import cn.interestingshop.entity.BaseOrder;
import cn.interestingshop.entity.Goods;
import cn.interestingshop.entity.OrderInfo;
import cn.interestingshop.service.baseOrder.BaseOrderService;
import cn.interestingshop.service.baseOrder.BaseOrderServiceImpl;
import cn.interestingshop.service.goods.GoodsService;
import cn.interestingshop.service.goods.GoodsServiceImpl;
import cn.interestingshop.service.orderInfo.OrderInfoService;
import cn.interestingshop.service.orderInfo.OrderInfoServiceImpl;
import cn.interestingshop.utils.EmptyUtils;
import cn.interestingshop.utils.Pager;
import cn.interestingshop.web.AbstractServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "OrderServlet",urlPatterns = "/manager/order")
public class OrderServlet extends AbstractServlet {
    @Override
    public Class getServletClass() {
        return OrderServlet.class;
    }

    public String index(HttpServletRequest request, HttpServletResponse response){



        String currentPage = request.getParameter("currentPage");
        int currentPageNo = EmptyUtils.isEmpty(currentPage)?1:Integer.valueOf(currentPage);



        BaseOrderService baseOrderService=new BaseOrderServiceImpl();

        int count = baseOrderService.selectOneAllCount(Integer.parseInt(request.getParameter("userId")));

        List<BaseOrder> list = baseOrderService.selectOneAll(Integer.parseInt(request.getParameter("userId")),currentPageNo,5);

        Pager pager = new Pager(count,5,currentPageNo);
        pager.setUrl("manager/order?action=index&userId="+request.getParameter("userId"));

        GoodsService goodsService=new GoodsServiceImpl();
        
        OrderInfoService orderInfoService=new OrderInfoServiceImpl();


        List<OrderInfo> list1=new ArrayList<>();

        List<BaseOrder> list2=new ArrayList<>();



        for (BaseOrder baseOrder : list) {

            List<OrderInfo> list3=new ArrayList<>();

            list1= orderInfoService.selectOne(baseOrder.getId());
            for (OrderInfo orderInfo : list1) {
                Goods goods = goodsService.selectOne(orderInfo.getGoodsId());
                orderInfo.setGoods(goods);
                list3.add(orderInfo);
            }
            baseOrder.setOrderInfoList(list3);
            list2.add(baseOrder);
        }




        request.setAttribute("orderList",list2);
        request.setAttribute("pager", pager);

        return "/manager/order/orderList";

    }


    public String queryAllOrder(HttpServletRequest request,HttpServletResponse response){

        String currentPage = request.getParameter("currentPage");
        int currentPageNo = EmptyUtils.isEmpty(currentPage)?1:Integer.valueOf(currentPage);


        BaseOrderService baseOrderService=new BaseOrderServiceImpl();

        int count = baseOrderService.selectAllCount();

        List<BaseOrder> list = baseOrderService.selectAll(currentPageNo,5);

        Pager pager = new Pager(count,5,currentPageNo);
        pager.setUrl("manager/order?action=queryAllOrder");

        GoodsService goodsService=new GoodsServiceImpl();

        OrderInfoService orderInfoService=new OrderInfoServiceImpl();


        List<OrderInfo> list1=new ArrayList<>();

        List<BaseOrder> list2=new ArrayList<>();



        for (BaseOrder baseOrder : list) {

            List<OrderInfo> list3=new ArrayList<>();

            list1= orderInfoService.selectOne(baseOrder.getId());
            for (OrderInfo orderInfo : list1) {
                Goods goods = goodsService.selectOne(orderInfo.getGoodsId());
                orderInfo.setGoods(goods);
                list3.add(orderInfo);
            }
            baseOrder.setOrderInfoList(list3);
            list2.add(baseOrder);
        }




        request.setAttribute("orderList",list2);
        request.setAttribute("pager", pager);

        return "/manager/order/orderList";
    }


    public String queryOrderInfo(HttpServletRequest request,HttpServletResponse response){
        OrderInfoService orderInfoService=new OrderInfoServiceImpl();
        GoodsService goodsService=new GoodsServiceImpl();

        List<OrderInfo> list = orderInfoService.selectOne(Integer.parseInt(request.getParameter("baseOrderId")));

        List<OrderInfo> list1=new ArrayList<>();

        for (OrderInfo orderInfo : list) {
            Goods goods = goodsService.selectOne(orderInfo.getGoodsId());
            orderInfo.setGoods(goods);
            list1.add(orderInfo);
        }

        request.setAttribute("orderInfoList",list1);

        return "/manager/order/orderInfoList";
    }
}
